<?php
/**
 *Copyright (c) <2017> <copyright taodingfei>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

namespace Zeus\Foundation;

use Zeus\Config\ConfigServiceProvider;
use Zeus\Container\AbstractContainer;
use Zeus\Contracts\Http\Kernel as HttpInterface;
use Zeus\Routing\Exceptions\RouteException;
use Zeus\Routing\RouterServiceProvider;
use Zeus\View\ViewServiceProvider;
use Zeus\Http\Response;

class Application extends AbstractContainer implements HttpInterface
{

    /**
     * Bootstrap the application for HTTP requests.
     *
     * @return void
     */
    public function bootstrap()
    {
        $this->setSystemService();
        $this->initSystem();
    }

    /**
     * Handle an incoming HTTP request.
     *
     * @param  \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function handle($request)
    {
        $router=$this->get("router");
        try{
            return Response::create($router->dispatch($request));
        }catch (RouteException $e){
            
        }
    }

    /**
     * Perform any final actions for the request lifecycle.
     *
     * @param  \Symfony\Component\HttpFoundation\Request $request
     * @param  \Symfony\Component\HttpFoundation\Response $response
     * @return void
     */
    public function terminate($request, $response)
    {

    }


    /**
     * Get the Zeus application instance.
     *
     * @return \Zeus\Container\AbstractContainer;
     */
    public function getApplication()
    {
        return $this;
    }

    /**
     * 注册系统必须服务
     */
    protected function setSystemService()
    {
        $this->addServiceProvider(new ConfigServiceProvider());
        $this->addServiceProvider(new RouterServiceProvider());
        $this->addServiceProvider(new ViewServiceProvider());
    }

    /**
     * 注册系统必须绑定
     */
    protected function setSystemBinds()
    {
        $this->share("app", $this);
        $this->share(self::class, $this);
        $this->share(\League\Container\ContainerInterface::class, $this);
    }

    /**
     * 执行系统初始化设置
     */
    protected function initSystem()
    {
        $this->setSystemBinds();
        $this->registerRoutes();
    }

    /**
     * 以默认方式注册系统路由
     */
    protected function registerRoutes()
    {
        if(($folderHandler=opendir($folder=$this->getRoutePath()))!==false){
            while (($file=readdir($folderHandler))!==false) {
                if($file!="." && $file!=".."){
                    call_user_func_array(
                        require_once $folder.DIRECTORY_SEPARATOR.$file, ["router"=>$this->get("router")]
                    );
                }
            }
        }else{
            call_user_func_array(
                require_once $folder.DIRECTORY_SEPARATOR."route.php", ["router"=>$this->get("router")]
            );
        }
    }
}
